package com.site.blog.my.core.controller.order;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.DefaultAlipayClient;
import com.alipay.api.request.AlipayTradePagePayRequest;
import io.swagger.annotations.Api;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @author Hujf
 * @title: OrderController
 * @date 2021/3/2818:34
 * @description: TODO
 */
@RestController
@RequestMapping(value = "/order")
public class OrderController {

    private static final String CHARSET = "UTF-8";
    //hujf2021paytest
    private static final String APP_PRIVATE_KEY = "MIIEvQIBADANBgkqhkiG9w0BAQEFAASCBKcwggSjAgEAAoIBAQCb6227DN0XxwSjgsfzy2xCPwNCmtl9MAZnv/2K2I7vrUXHm4NRKO0vOmzVaJhy24EoEkrh9u/UdM4+sKF1ydof/Z2SPbURhNPbx0/VmSGNOCRZ0tEo8d4JbQmxnvv0/PsdGv2T0JazIT2Xj4O41PUJX5xfasxI8g6DVHHmasDwW+SnrLiEQ2wfaxev6EwT6a0IbFL8HdL95ieYKh72HN2G0fak/ApUfdVUXyZKHEFmHJxl/CSpIUHu01qmoKh9ANCIrb9QLB8sR+pK637KYcxr2jlQhlbqDyRMRKinRYmZcVhH7PihD1qTi770HpBLbnRDpxHDjURWsIQmqlR7vOpZAgMBAAECggEAaOR8QJ8Jyio4Q6wCyk8quHJdxhwYfnCljnyJnDPODQ27aOqE8D+hOOmMOBGSpYpoHEYaHGQcuggOi/wV0wEFfgoHTE+5FV0PjcZLPf4zpSaExezeizYT0rGJGPZEuG7MnKsAtsVX7JBLOD/zNJAHfNPWhhZ58gTtoIpZydI9lDScuHdsKFwXCv7Z17NGj68kr6LKvy3lzk5i0DKgAtTvPTfP1O+HYSQkErTIUxhIfrX2fT/c7l27GuvIFEgL24RmOf4UIgYC0xryb2Z1m6FA+P7LpD5WMXcI8XxFv9MzmF7cIqbrGRhC1wfcW/SOYRMlYHrHjBu3LnSD4/EwzFHbhQKBgQDvGlRWu7cqTiS5+Kz8cEFplT3eeYaQHgfNZPRPxq4oyI+4CslxRK0xh/ktpvEkUmrVU4brTweUjWvAYZjMbBfwYui5K6EamQCFktoomIbztHM9oVvry6ZXaM3mkbTI49CrRZKqHbnFaEB0uIDMPQnmD0W6eDDoE5uWNX0sNUwo+wKBgQCm8DXqyZvorq/A/DYi00/68jwyXQT8TUc0TEwZWSbqMkVlq2TroxSkweO3G0zRB7IYh4T50QwQcjWNFrNONKoKMI5j9+b+IqKk7i2zev5ZcxFSqJxAR7EbBFU4Ay0+0+lPuFIEPKx2/E3phxm2wvnKN2AdnSePj5hNzEOwFDYBuwKBgBRJ3qgxa3xqfxFMuZj+CX7lDAfptfJaqfzSuSxfxn1tb0yFlcxHsI6IZCtsg/+6Apixarxiy1jemysK3QVTksByFB9KRXRVUUcETACv2JurTzAGlh8u+6FlIjuTV4zIT/eIgsZumUnX68g7s6og78sWtqr48eVvbH9VcJakux6rAoGAMbjaVlQ2W9esAY8dkMHf0J7VzyplkcBytOEEyYgETz0p7l5ecNDiFTCq4p7/AkOIiEXVFZNuRHe/+4YHYgPNobtUrfHom0k1qGpIsmyv1XSwdC5EW+D+n01rFIPdthjoJRaBV0G7HLWJf0T+I7FvNQvJVQi5/8iFVKbCgm+CpoECgYEAm2RL3MY6nfWfYgiCRRDZYTtOH9pnOqO0J9mUVuL4gATHfApeuR2WLhOtwh/W1hyVxONOVIHab8Wjr4vY4WnAwTL2RUG4m0zO5lzu2nBI/xx3q3wfLkIkhZBfrXVXsWXMEeEAYB++TtyiYmunHOm0poli2p9kB+r70KUc4N7gBH0=";
    private static final String APP_ID = "2021002134695448";
    private static final String FORMAT = "json";
    private static final String ALIPAY_PUBLIC_KEY = "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAm+ttuwzdF8cEo4LH88tsQj8DQprZfTAGZ7/9itiO761Fx5uDUSjtLzps1WiYctuBKBJK4fbv1HTOPrChdcnaH/2dkj21EYTT28dP1ZkhjTgkWdLRKPHeCW0JsZ779Pz7HRr9k9CWsyE9l4+DuNT1CV+cX2rMSPIOg1Rx5mrA8Fvkp6y4hENsH2sXr+hME+mtCGxS/B3S/eYnmCoe9hzdhtH2pPwKVH3VVF8mShxBZhycZfwkqSFB7tNapqCofQDQiK2/UCwfLEfqSut+ymHMa9o5UIZW6g8kTESop0WJmXFYR+z4oQ9ak4u+9B6QS250Q6cRw41EVrCEJqpUe7zqWQIDAQAB";
    private static final String SIGN_TYPE = "RSA2";

    @RequestMapping(value = "/doPost")
    public   void   doPost (HttpServletRequest httpRequest,
                            HttpServletResponse httpResponse)   throws ServletException, IOException {
        AlipayClient alipayClient =  new DefaultAlipayClient( "https://openapi.alipay.com/gateway.do" , APP_ID, APP_PRIVATE_KEY, FORMAT, CHARSET, ALIPAY_PUBLIC_KEY, SIGN_TYPE);  //获得初始化的AlipayClient
        AlipayTradePagePayRequest alipayRequest =  new  AlipayTradePagePayRequest(); //创建API对应的request
        alipayRequest.setReturnUrl( "http://domain.com/CallBack/return_url.jsp" );
        alipayRequest.setNotifyUrl( "http://domain.com/CallBack/notify_url.jsp" ); //在公共参数中设置回跳和通知地址
        alipayRequest.setBizContent( "{"  +
                "    \"out_trade_no\":\"20150320010101001\","  +
                "    \"product_code\":\"FAST_INSTANT_TRADE_PAY\","  +
                "    \"total_amount\":0.01,"  +
                "    \"subject\":\"Iphone6 16G\","  +
                "    \"body\":\"Iphone6 16G\","  +
                "    \"passback_params\":\"merchantBizType%3d3C%26merchantBizNo%3d2016010101111\","  +
                "    \"extend_params\":{"  +
                "    \"sys_service_provider_id\":\"2088511833207846\""  +
                "    }" +
                "  }" ); //填充业务参数
        String form= "" ;
        try  {
            form = alipayClient.pageExecute(alipayRequest).getBody();  //调用SDK生成表单
        }  catch  (AlipayApiException e) {
            e.printStackTrace();
        }
        httpResponse.setContentType( "text/html;charset="  + CHARSET);
        httpResponse.getWriter().write(form); //直接将完整的表单html输出到页面
        httpResponse.getWriter().flush();
        httpResponse.getWriter().close();
    }
}
